/*
 * ComerciosImpuestosRegistrosFrame.java
 *
 * Created on 24 de abril de 2008, 06:49 PM
 */
package org.comercios.registros;

import org.mybeans.Sesion;
 
import org.mybeans.ComerciosBeans;
import org.mybeans.RegimenComerciosBeans;
import org.comercios.registros.ComerciosBusquedaFrame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import org.menu.MenuComercios;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author  avbravo
 */
public class ComerciosImpuestosRegistrosFrame extends javax.swing.JFrame {

    boolean focoComercio = false;
    boolean focoImpuestos = false;
   
    Sesion sesion;
    ComerciosBeans comerciosBeans;
    RegimenComerciosBeans regimenComerciosBeans;
    int fila = -1;
    int Anio;
    int NumeroMes;
    DefaultTableModel modelo;
    String IdComercio;

    /** Creates new form ContribuyentesImpuestosVerFrame */
    public ComerciosImpuestosRegistrosFrame() {
        initComponents();
        sesion = new Sesion();


        /*
         * Fondo
         */

         
         
         

        jPanel3.setOpaque(false);
        setContentPane(jPanel3);

        /*
         * fin del fondo
         */
        comerciosBeans = new ComerciosBeans();
        regimenComerciosBeans = new RegimenComerciosBeans();

        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        this.Anio = ca.get(java.util.Calendar.YEAR);
        NumeroMes = ca.get(java.util.Calendar.MONTH);

        sesion.setAnio(this.Anio);

        modelo = (DefaultTableModel) jXTable1.getModel();


        TableColumn columnId = jXTable1.getColumnModel().getColumn(0);
        columnId.setPreferredWidth(130);

        TableColumn columnRegimen = jXTable1.getColumnModel().getColumn(1);
        columnRegimen.setPreferredWidth(130);



        /*
         * Action del menu
         */

       

        Limpiar();
        if (sesion.getIdComercio() == null || sesion.getIdComercio().equals("")) {
        } else {
            jTextFieldIdComercio.setText(sesion.getIdComercio());
            ValidarComercio();
        }
  MenuComercios menuComercios = new MenuComercios();
        menuComercios.VerificarPrivilegios();


        if (menuComercios.isBjMenuItemComerciosImpuestosAgregar()) {
            jButtonAgregar.setVisible(true);
        } else {
            jButtonAgregar.setVisible(false);
        }


        if (menuComercios.isBjMenuItemComerciosImpuestosEliminar()) {
            jButtonEliminar.setVisible(true);

        } else {
            jButtonEliminar.setVisible(false);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldCedula = new javax.swing.JTextField();
        jTextFieldContribuyente = new javax.swing.JTextField();
        jButtonRegresar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldIdRegimen = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldRegimen = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldValor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldIdComercio = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldComercio = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jButtonVistaPrevia1 = new javax.swing.JButton();
        jButtonImprimir = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButtonAgregar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registros de Impuestos de Comercios");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Cedula");

        jLabel2.setText("Contribuyente");

        jTextFieldCedula.setEditable(false);
        jTextFieldCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCedulaActionPerformed(evt);
            }
        });

        jTextFieldContribuyente.setEditable(false);

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setMnemonic('r');
        jButtonRegresar.setToolTipText("Cerrar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Regimen", "Valor", "Periodo", "Mes", "Inscripcion"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jXTable1.setOpaque(false);
        jScrollPane1.setViewportView(jXTable1);

        jLabel5.setText("Codigo");

        jTextFieldIdRegimen.setEditable(false);

        jLabel6.setText("Regimen");

        jTextFieldRegimen.setEditable(false);

        jLabel7.setText("Valor");

        jTextFieldValor.setEditable(false);
        jTextFieldValor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldValorActionPerformed(evt);
            }
        });

        jLabel4.setText("Codigo");

        jTextFieldIdComercio.setColumns(35);
        jTextFieldIdComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdComercioActionPerformed(evt);
            }
        });

        jLabel8.setText("Comercio");

        jTextFieldComercio.setEditable(false);

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonVistaPrevia1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/preview.png"))); // NOI18N
        jButtonVistaPrevia1.setToolTipText("Vista Previa");
        jButtonVistaPrevia1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVistaPrevia1ActionPerformed(evt);
            }
        });

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/print.jpeg"))); // NOI18N
        jButtonImprimir.setToolTipText("Imprimir");
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButton2.setToolTipText("Regresar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButtonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/impuestos.png"))); // NOI18N
        jButtonAgregar.setToolTipText("Agregar impuestos");
        jButtonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAgregarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(58, 58, 58)
                        .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel4))
                        .addGap(38, 38, 38)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButtonAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonRegresar))
                            .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 497, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(93, 93, 93))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonVistaPrevia1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(56, 56, 56)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonBuscar)
                        .addComponent(jButtonRegresar))
                    .addComponent(jButtonAgregar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonVistaPrevia1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextFieldValorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldValorActionPerformed
}//GEN-LAST:event_jTextFieldValorActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jTextFieldCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCedulaActionPerformed
}//GEN-LAST:event_jTextFieldCedulaActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

        if (focoComercio) {

            Limpiar();
            CargarComercios();
            CargarImpuestos();
        }else{
            if (focoImpuestos){
                CargarImpuestos();
            }
        }

        focoComercio = false;

    focoImpuestos = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "formWindowGainedFocus()\nErro: " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

    public void CargarComercios() {
        try {
            // ContribuyentesBeans contribuyentesBeans = new ContribuyentesBeans();
            if (comerciosBeans.getActivo().equals("no")) {
                JOptionPane.showMessageDialog(this, "Este registro esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            this.jTextFieldIdComercio.setText(comerciosBeans.getIdComercio());
            sesion.setIdComercio(jTextFieldIdComercio.getText());
            this.jTextFieldContribuyente.setText(comerciosBeans.getContribuyente());
            this.jTextFieldComercio.setText(comerciosBeans.getComercio());
            jTextFieldCedula.setText(comerciosBeans.getCedula());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

private void jTextFieldIdComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdComercioActionPerformed
    ValidarComercio();
}//GEN-LAST:event_jTextFieldIdComercioActionPerformed

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

    ComerciosBusquedaFrame comerciosBusquedaFrame = new ComerciosBusquedaFrame();
    comerciosBusquedaFrame.setLocationRelativeTo(null);
    comerciosBusquedaFrame.setVisible(true);
    focoComercio = true;
    focoImpuestos = false;
}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jButtonVistaPrevia1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVistaPrevia1ActionPerformed


    String IdComercio = this.jTextFieldIdComercio.getText();
    if (IdComercio== null || IdComercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el codigo del comercio ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String comercio = this.jTextFieldComercio.getText();
    if (comercio == null || comercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ComerciosImpuestos.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "ComerciosImpuestos.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_IDCOMERCIO", IdComercio);
        params.put("P_MUNICIPIO", sesion.getMunicipio());

        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonVistaPrevia1ActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed


    String IdComercio = this.jTextFieldIdComercio.getText();
    if (IdComercio== null || IdComercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el codigo del comercio ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String comercio = this.jTextFieldComercio.getText();
    if (comercio == null || comercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ComerciosImpuestos.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "ComerciosImpuestos.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_IDCOMERCIO", IdComercio);
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperPrintManager.printReport(jasperPrint, true);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jButtonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAgregarActionPerformed
    // TODO add your handling code here:
       this.IdComercio = this.jTextFieldIdComercio.getText();
    if (this.IdComercio == null || this.IdComercio.equals("")) {
        JOptionPane.showMessageDialog(this, "seleccione un comercio ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }
    sesion.setIdComercio(this.IdComercio);
    ComerciosImpuestosInscripcionAgregarFrame comerciosImpuestosInscripcionAgregarFrame = new ComerciosImpuestosInscripcionAgregarFrame();
   comerciosImpuestosInscripcionAgregarFrame.setLocationRelativeTo(null);
    comerciosImpuestosInscripcionAgregarFrame.setVisible(true);
    focoComercio = false;
    focoImpuestos = true;
}//GEN-LAST:event_jButtonAgregarActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jButton2ActionPerformed

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
    // TODO add your handling code here:
    try {


            String IdRegimen = jTextFieldIdRegimen.getText();

            if (IdRegimen == null || IdRegimen.equals("")) {
                sesion.MostrarMensajeAdvertencia("Mensaje", "Seleccione un regimen");
                return;
            }
            String Regimen =  jTextFieldRegimen.getText();
            if (Regimen == null || Regimen.equals("")) {
                sesion.MostrarMensajeAdvertencia("Mensaje", "Seleccione un regimen");
                return;
            }
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            //poner el estatus a inactivo
            int res = JOptionPane.showConfirmDialog(this, "Desea eliminar este regimen " + IdRegimen + "\n Regimen: " + Regimen,
                    "Eliminar Regimen ", JOptionPane.YES_NO_OPTION);
            if (res == JOptionPane.YES_OPTION) {

                String sql = " delete from regimencomercios  where idmunicipio=? and idcomercio= ? and idregimen=? and activo='si' ";
                PreparedStatement prest = sesion.con.prepareStatement(sql);
                prest.setString(1, sesion.getIdmunicipio());
                prest.setString(2, sesion.getIdComercio());
                prest.setString(3, IdRegimen);
                System.out.println("ax sql " + sql);
                prest.executeUpdate();

                java.util.Calendar ca = java.util.Calendar.getInstance();
            java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());

            int Anio = ca.get(java.util.Calendar.YEAR);


            //pagosmensuales
            sql = " delete from  pagosmensuales "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=? and activo='si' ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();


            //pagosanuales
            sql = " delete from  pagosanuales "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=? and activo='si' ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();


             //morosidadmensual
            sql = " delete from  morosidadmensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

              //recargomensual
            sql = " delete from  recargomensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();


             //cierremorosidadmensual
            sql = " delete from  cierremorosidadmensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

//cierremorosidadmensualanual
            sql = " delete from  cierremorosidadmensualanual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();

             //cierrerecargomensual
            sql = " delete from  cierrerecargomensual "
                    + " where idmunicipio=? and id= ? and tipoid=? "
                    + " and anio= ? and idregimen=?  ";
            prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, sesion.getIdComercio());
            prest.setString(3, "comercios");
            prest.setInt(4, Anio);
            prest.setString(5, IdRegimen);
            prest.executeUpdate();
                sesion.con.close();
                JOptionPane.showMessageDialog(this, "Regimen eliminando exitosamente", "Eliminado", JOptionPane.INFORMATION_MESSAGE);
                CargarImpuestos();
            } else {
                JOptionPane.showMessageDialog(this, "No se elimino el regimen.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "jMenuItemPagarActionPerformed()\n " + ex.getMessage().toString());
        }
}//GEN-LAST:event_jButtonEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosImpuestosRegistrosFrame().setVisible(true);
            }
        });
    }

    private void ValidarComercio() {
        try {
            String idComercio = this.jTextFieldIdComercio.getText();
            comerciosBeans.setIdComercio(idComercio);

            if (comerciosBeans.Consultar() == true) {
                CargarComercios();
                CargarImpuestos();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

  

    public void CargarImpuestos() {
        try {
            Limpiar();
            String idcomercio = jTextFieldIdComercio.getText();
            String sql = "select regimencomercios.*, regimen.regimen from  regimencomercios, regimen where regimencomercios.idmunicipio=? and regimencomercios.idcomercio =? and regimencomercios.activo='si' and regimencomercios.idregimen = regimen.idregimen and regimen.idmunicipio=?";
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            psquery.setObject(1, sesion.getIdmunicipio());
            psquery.setObject(2, idcomercio);
            psquery.setObject(3, sesion.getIdmunicipio());
            ResultSet rs = psquery.executeQuery();



// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[6]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idregimen");
                fila[1] = rs.getString("regimen");
                fila[2] = rs.getString("valor");
                fila[3] = rs.getString("periodopago");
                fila[4] = rs.getString("mespago");
                java.util.Calendar ca = java.util.Calendar.getInstance();
                java.sql.Date mydate = rs.getDate("fechainscripcion");
                //fila[5] = rs.getDate("fechainscripcion");
                Format formatter;
                formatter = new SimpleDateFormat("dd/MM/yyyy");
                String s = formatter.format(mydate);
                fila[5] = s;
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);

            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e)) {

                    }

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                        String idRegimen = String.valueOf(jXTable1.getValueAt(fila, 0));
                        String regimen = String.valueOf(jXTable1.getValueAt(fila, 1));
                        String Svalor = String.valueOf(jXTable1.getValueAt(fila, 2));
                        Double valor = Double.parseDouble(Svalor);
                        String periodoPago = String.valueOf(jXTable1.getValueAt(fila, 3));
                        String mesPago = String.valueOf(jXTable1.getValueAt(fila, 4));
                        regimenComerciosBeans.setIdRegimen(idRegimen);
                        regimenComerciosBeans.setRegimen(regimen);
                        regimenComerciosBeans.setValor(valor);
                        regimenComerciosBeans.setPeriodoPago(periodoPago);
                        regimenComerciosBeans.setMesPago(mesPago);
                        jTextFieldIdRegimen.setText(idRegimen);
                        jTextFieldRegimen.setText(regimen);
                        jTextFieldValor.setText(String.valueOf(valor));

                    }
                }
            });
            TableColumn column = jXTable1.getColumnModel().getColumn(1);
            column.setPreferredWidth(250);
            sesion.con.close();
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error", "ContribuyentesVerFrame.java\nCargarImpuestos()" + ex.getMessage().toString());
        }

    }

    private void Limpiar() {
        jTextFieldIdRegimen.setText("");
        jTextFieldRegimen.setText("");
        jTextFieldValor.setText("");
        LimpiarTable();
    }

    private void LimpiarTable() {
        try {
            //limpiar  el jXTable

            boolean control = false;
            while (control == false) {
                int n = jXTable1.getRowCount();
                //System.out.println("n" + n);
                if (n <= 0) {
                    control = true;
                } else {
                    modelo.removeRow(0);
                }

            }//while
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error", "LimpiarTable()" + ex.getMessage().toString());
        }
    }

   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonAgregar;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVistaPrevia1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldCedula;
    private javax.swing.JTextField jTextFieldComercio;
    private javax.swing.JTextField jTextFieldContribuyente;
    private javax.swing.JTextField jTextFieldIdComercio;
    private javax.swing.JTextField jTextFieldIdRegimen;
    private javax.swing.JTextField jTextFieldRegimen;
    private javax.swing.JTextField jTextFieldValor;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
